package com.rimi.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rimi.bean.User;
import com.rimi.service.UserImageService;
import com.rimi.service.UserService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/uploadImage")
public class UploadImageController {

    @Autowired
    private UserImageService userImageService;

    @RequestMapping("/addShowImage")
    public String showImage(@RequestParam MultipartFile fileName, HttpServletRequest request, String userId , Model model) {
        // 上传文件后获取文件的存储地址
        String name = fileName.getOriginalFilename();
        System.out.println(name);
        System.out.println(userId+"------");
        System.out.println(fileName);
        String fileUrl = userImageService.addUserImageUrl(request, userId, fileName);
        // 创建一个新的userImage对象，并将属性添加上去
        User user = new User();
        user.setUserHardUrl(fileUrl);
        // 将对象添加到数据表中
        boolean insert = userImageService.update(user,new EntityWrapper<User>().eq("user_id",userId));
        User user1 = userImageService.selectById(userId);
        request.getSession().setAttribute("user",user1);

        // 判断是否上传成功
        System.out.println("文件是否上传成功：" + insert);

        return "redirect:/user3/in";
    }


}
